package com.serial.comm.core.telegram.builder;

import com.serial.comm.core.constants.SystemConstants;
import com.serial.comm.core.exception.*;
import com.serial.comm.core.exception.enums.ResponseCodeEnum;
import com.serial.comm.core.utils.Pair;
import com.serial.comm.core.utils.TelegramUtils;
import com.serial.comm.modular.serialPort.model.Telegram;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.serial.comm.core.constants.TelegramConst.*;

/**
 * 具体的报文构造者
 */
public class AftnBuilder extends Builder {

    public static Logger log = LoggerFactory.getLogger(AftnBuilder.class);

    Telegram telegram = new Telegram();


    public AftnBuilder(String telegramText) {
        super(telegramText);
    }


    @Override
    public void buildHeader() {
        SimpleDateFormat sdf = new SimpleDateFormat(SystemConstants.Pattern.DATE_FORMAT_1);
        try {
            String recDateStr = TelegramUtils.extractRecDate(telegramText, START_FLAG_HEADER, END_FLAG);
            Date recDate = sdf.parse(recDateStr);
            telegram.setRecDate(recDate);
        } catch (ParseException e) {
            throw new ExtractHeaderFailure(ResponseCodeEnum.EXTRACT_HEADER_FAILURE);
        }
    }

    @Override
    public void buildGUID() {
        try {
            String guid = TelegramUtils.extractGuid(telegramText, START_FLAG_GUID, END_FLAG);
            telegram.setGuid(guid);
        } catch (Exception e) {
            throw new ExtractGUIDFailure(ResponseCodeEnum.EXTRACT_GUID_FAILURE);
        }
    }

    @Override
    public void buildPriority() {
        try {
            String priority = TelegramUtils.extractPriority(telegramText, START_FLAG_PRIORITY, END_FLAG);
            telegram.setPriority(priority);
        } catch (Exception e) {
            throw new ExtractPriorityFailure(ResponseCodeEnum.EXTRACT_PRIORITY_FAILURE);
        }
    }

    @Override
    public void buildOrigin() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(SystemConstants.Pattern.DATE_FORMAT_2);
            Pair<String, String> originPair = TelegramUtils.extractOrigin(telegramText, START_FLAG_ORIGIN, END_FLAG);
            telegram.setOrigin(originPair.getFirst());
            telegram.setSendDate(originPair.getSecond());
        } catch (Exception e) {
            throw new ExtractOriginFailure(ResponseCodeEnum.EXTRACT_ORIGIN_FAILURE);
        }

    }

    @Override
    public void buildDestination() {
        try {
            String destination = TelegramUtils.extractDestination(telegramText, START_FLAG_DESTINATION, END_FLAG, telegram.getPriority());
            telegram.setDestination(destination);
        } catch (Exception e) {
            throw new ExtractDestinationFailure(ResponseCodeEnum.EXTRACT_DESTINATION_FAILURE);
        }
    }

    @Override
    public void buildText() {
        String text = TelegramUtils.extractText(telegramText, START_FLAG_TEXT, TERMINATOR_FLAG);
        telegram.setText(text);


    }

    @Override
    public Telegram getTelegram() {
        return telegram;
    }


}
